var app = getApp();
const Zan = require('../../dist/index');
var Const = require('./../../utils/const.js');

Page(Object.assign({}, Zan.TopTips, {

	data: {
		balance: 0,
		rechargable: false,
		selected: null,
		rechargeItems: []
	},

	itemSelect(e) {
		var that = this;
		var index = e.currentTarget.dataset.index;
		var rechargeItems = that.data.rechargeItems;
		var changed = {};

		for (var i in rechargeItems) {
			if (index == i) {
				changed['rechargeItems[' + i + '].checkClass'] = ' checked';
			} else {
				changed['rechargeItems[' + i + '].checkClass'] = '';
			}
		}

		if (index != null && index != undefined) {
			that.setData(Object.assign(changed, {
				selected: rechargeItems[index],
				rechargable: true
			}));

		} else {
			that.setData(Object.assign(changed, {
				selected: null,
				rechargable: false,
				rechargeItems: rechargeItems
			}));
		}
	},

	onLoad: function (options) {
		this.getRuleList();
	},

	onReady: function () {
		this.userinfo();
	},

	onShow: function () {

	},

	pay: function () {
		var that = this;

		if (that.data.rechargable) {
			// get price
			var total = that.data.selected.actualCharge, rechargeRuleId = that.data.selected.id, orderId;
			if (that.data.selected && total > 0) {
				wx.request({
					url: 'https://tianheng.xlxiot.com/wx/recharge/prePay',
					data: {
						total: total,
						rechargeRuleId: rechargeRuleId,
						ticket: app.data.token
					},
					success: function (response) {
						if (response.statusCode == 200) {
							if (response.data.hasOwnProperty('code')) {
								var code = response.data.code;
								if (code == Const.SUCCESS.DEFAULT) {
									var _data = response.data.data;
									orderId = _data.orderId;
									console.log(_data);
									// 发起微信支付
									wx.requestPayment({
										'timeStamp': _data.timeStamp,
										'nonceStr': _data.nonceStr,
										'package': _data.package,
										'signType': _data.signType,
										'paySign': _data.paySign,
										'success': function (res) {
											wx.navigateBack({
												delta: 1
											});
											console.log(res);
										},
										'fail': function (res) {
											console.log(res);
											if (res.errMsg = 'requestPayment:cancel') {
												console.log(orderId);
												// cancel payorder
												app.battery.common('payorder/cancel', {
													ticket: app.data.token,
													id: orderId
												}).then(function (d) {
													console.log(d);
												}).catch(function (e) {
													that.showZanTopTips(e.message, {type: 'error'});
												});
											}
										}
									});
									console.log('-----支付结束-----');
								} else {
									that.showZanTopTips('网络异常，稍后再试', {type: 'error'});
								}
							}
						} else {
							that.showZanTopTips('网络异常，稍后再试', {type: 'error'});
						}
						console.log('-----TEST Flag-----');
					}
				})
			} else {
				that.showZanTopTips('金额不得小于0', {type: 'error'});
			}
		}
	},

	getRuleList: function () {
		var that = this;
		var rechargeItems = [];

		app.battery.common('rule/list', {type: 1}).then(function (d) {
			d.forEach(v => {
				rechargeItems.push({
					id: v.id,
					actualCharge: v.actualCharge / 100.0,
					presentedCharge: v.presentedCharge / 100.0
				})
			});

			that.setData({
				rechargeItems: rechargeItems
			})
		}).catch(function (e) {
			that.showZanTopTips(e.message, {type: 'error'});
		});
	},

	userinfo: function () {
		var that = this;

		app.battery.findOne('member/detail', app.data.member.id).then(function (r) {
			that.setData({
				balance: (r.walletMoney - r.tradeMoney).toFixed(2)
			});
		}).catch(function (e) {
			that.showZanTopTips(e.message, {type: 'error'});
		});
	},
}));